
const express = require('express');
const cors = require('cors');
const app = express();
const PORT = 3000;

// 允许前端跨域
app.use(cors());
// 解析 application/x-www-form-urlencoded（form 默认格式）
app.use(express.urlencoded({ extended: false }));

// GET 接口  /user/get?username=xxx&password=xxx
app.get('/user/get', (req, res) => {
  const { username, password } = req.query;
  console.log('[GET] 收到：', { username, password });
  res.send(`GET 成功 → 用户名：${username}`);
});

// POST 接口  /user/post
app.post('/user/post', (req, res) => {
  const { username, password } = req.body;
  console.log('[POST] 收到：', { username, password });
  res.send(`POST 成功 → 用户名：${username}`);
});

// 把 public 文件夹挂成静态文件，方便访问 index.html
app.use(express.static('public'));

app.listen(PORT, () => console.log(`Express 跑在 http://localhost:${PORT}`));
